// created by noellynch
// Mar 10, 2011

package com.apachepivot.utils;

import java.net.URL;

import org.apache.pivot.beans.BXMLSerializer;

public class BXMLUtils {
//	static	public	<T>T		readObject(URL cl_location) throws Exception {
//		BXMLSerializer		lcl_reader = new BXMLSerializer();
//		return (T)lcl_reader.readObject(cl_location);
//	}
	
	@SuppressWarnings("unchecked")
	static	public	<T>T		readObject(URL cl_location, Object ... cla_objs) throws Exception {
		BXMLSerializer		lcl_reader = new BXMLSerializer();
		
		if(cla_objs != null) {
			for(int li_index = 0; li_index < cla_objs.length;) {
				lcl_reader.getNamespace().put(cla_objs[li_index].toString(), cla_objs[li_index + 1]);
				li_index += 2;
			}
		}
		
		return (T)lcl_reader.readObject(cl_location);
	}
	
	@SuppressWarnings("unchecked")
	static	public	<T>T		readObject(String str_location, Class<?> cl_class, Object ... cla_objs) throws Exception {
		URL		lcl_url = getResourceFromClass(cl_class, str_location);
		System.out.println("url from class loader for read object " + lcl_url);
		return BXMLUtils.<T>readObject(lcl_url, cla_objs);
	}
	
	static	public	URL		getResourceFromClassLoader(Object cl_obj, String str_res) {
		return cl_obj.getClass().getClassLoader().getResource(str_res);
	}
	
	static	public	URL		getResourceFromClass(Class cl_obj, String str_res) {
		return cl_obj.getClassLoader().getResource(str_res);
	}
}
